package com.jerei.web.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jerei.utils.spring.PageBean;

import java.util.List;

/**
 * Created by sunfeihu on 2016/10/21.
 */
public class UtilsUI {

    public static String tip(String message) {
        JSONObject rs = new JSONObject();
        rs.put("tip", message);
        return rs.toJSONString();
    }

    public static String ok() {
        return ok(null);
    }

    public static String ok(Object obj) {
        JSONObject rs = new JSONObject();
        rs.put("rs", "ok");
        if (null != obj) {
            rs.put("output", obj);
        }
        return rs.toJSONString();
    }

    public static String fail() {
        return fail(null);
    }

    public static String fail(Object obj) {
        JSONObject rs = new JSONObject();
        rs.put("rs", "fail");
        if (null != obj) {
            rs.put("output", obj);
        }
        return rs.toJSONString();
    }

    public static String datagrid() {
        JSONObject rs = new JSONObject();
        rs.put("total", 0);
        rs.put("rows", new JSONArray());
        return rs.toJSONString();
    }

    public static String datagrid(PageBean<?> pageBean) {
        JSONObject rs = new JSONObject();
        if (null != pageBean) {
            rs.put("total", pageBean.getTotalRows());
            rs.put("rows", pageBean.getResultList());
        } else {
            rs.put("total", 0);
            rs.put("rows", new JSONArray());
        }
        return rs.toJSONString();
    }

    public static String datagrid(List<?> list) {
        JSONObject rs = new JSONObject();
        if (null != list) {
            rs.put("total", list.size());
            rs.put("rows", list);
        } else {
            rs.put("total", 0);
            rs.put("rows", new JSONArray());
        }
        return rs.toJSONString();
    }

}